<?php

class Loginctrl extends Controller
{
    
    function Loginctrl()
    {
        parent::Controller();
        
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('email');
        
        $this->load->library('form_validation');
        
        $this->load->model('ignitedrecord/ignitedrecord');
        $this->load->model('login');
    }
    
    function index()
    {
        $this->load->view("login_view");
    }
    
    function forgot_pass()
    {
        $this->load->view("forgot_pass_view", array('success' => NULL));
    }
    
    function new_pass()
    {
        $this->load->view("new_pass_view");
    }

    function forgot_pass_submit()
    {
        $this->form_validation->set_rules('login', 'Login', 'trim|required|alpha_numeric');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
        
        if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('forgot_pass_view', array('success' => NULL));
		}
		else
		{
            $success = $this->login->check_valid_email($_POST['login'], $_POST['email']);       
     
            if(! $success):
                $this->load->view('forgot_pass_view', array('success' => False));
                return;
            endif;

            // wyślij email...

            $this->load->view('forgot_pass_view', array('success' => True));
		}
    }
    
    function check_login($login)
    {
        // tu sprawdzamy czy dany login istnieje i zwracamy maila albo null jeśli nie istnieje
        return 'tinez@tlen.pl';
        //return NULL;
        
    }
    
    function check_password()
    {
        // tu sprawdzamy poprawność loginu i hasła
        $this->load->model('login');
        $id = $this->login->check_valid_pass($_POST['login'], $_POST['password']);       
 
        if($id == NULL):
            $this->load->view('wrong_pass');
            return;
        endif;
                
        $this->check_user_type($id);
    }
    
    function check_user_type($id)
    {
        // tu sprawdzamy czy ktoś jest uczniem/nauczycielem/rodzicem/administrator
        // i otwieramy odpowiedni interfejs
        
        $this->load->model('login');
        $result = $this->login->check_user_type($id);
        
        if($result == NULL):
            $this->load->view('wrong_pass');
        endif;

        switch($result['type']):
            case 'student':
                redirect('students/panel/'.$result['firstname'].'/'.$result['secondname']);
                break;
            case 'teacher':
                redirect('teachers/znajdzklasy/'.$result['firstname'].'/'.$result['secondname']);
                break;
            case 'parentt':
                redirect('parents/znajdzdzieci/'.$result['firstname'].'/'.$result['secondname']);
                break;
            case 'admin':
                redirect('admin/admin');
                break;
        endswitch;
    }
    
    function change_pass()
    {
        $this->load->view('change_pass_view', array('match' => NULL));
    }
    
    function password_match($pass1, $pass2)
    {
        print "nowe hasła: $pass1, $pass2\n";
        $data['match'] = ($pass1 == $pass2);
        print "match = {$data['match']}\n";
        $this->load->view('change_pass_view', $data);
    }
    
    function change_pass_submit()
    {
        $this->form_validation->set_rules('login', 'Login', 'trim|required|alpha_numeric');
        $this->form_validation->set_rules('password', 'Hasło', 'trim|required|alpha_numeric');
        $this->form_validation->set_rules('new_pass1', 'Nowe hasło', 'trim|required|alpha_numeric|matches[new_pass2]');
        $this->form_validation->set_rules('new_pass2', 'Powtórz nowe hasło', 'trim|required|alpha_numeric');
        
        if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('change_pass_view', array('match' => NULL));
		}
		else
		{
            $id = $this->login->check_valid_pass($_POST['login'], $_POST['password']);       
     
            if($id == NULL):
                $this->load->view('wrong_pass');
                return;
            endif;

            $this->login->change_pass($id, $_POST['new_pass1']);

			$this->load->view('change_pass_view', array('match' => True));
		}
    }
}

?>
